package com.tencent.yolov5ncnn

import android.graphics.SurfaceTexture
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.view.LayoutInflater
import android.view.TextureView
import android.view.TextureView.SurfaceTextureListener
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.tencent.yolov5ncnn.VLCPlayer.VLCPlayerCallback
import java.io.File
import java.io.FileWriter

class monitor : Fragment(), VLCPlayerCallback {
    private var textureView: TextureView? = null
    private var root: View? = null

    private var vlcPlayer: VLCPlayer? = null
    private val isRecording = false

    //private static final String rtspUrl = "https://vfx.mtime.cn/Video/2019/07/12/mp4/190712140656051701.mp4";
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        if (root == null) {
            root = inflater.inflate(R.layout.fragment_monitor, container, false)
        }
        textureView = root!!.findViewById(R.id.video_view)
        val takeScreen = root!!.findViewById<Button>(R.id.takePic)
        val connectWifi = root!!.findViewById<Button>(R.id.findWifi)
        //        initVLCPlayer();
        textureView?.setSurfaceTextureListener(object : SurfaceTextureListener {
            override fun onSurfaceTextureAvailable(
                surfaceTexture: SurfaceTexture,
                i: Int,
                i1: Int
            ) {
                initVLCPlayer()
            }

            override fun onSurfaceTextureSizeChanged(
                surface: SurfaceTexture,
                width: Int,
                height: Int
            ) {
            }

            override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
                return false
            }

            override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {
            }
        })
        takeScreen.setOnClickListener {   val picturePath =
                MediaFileUtils.getOutputMediaFile(true)
            //                new Thread(() -> {
//                    vlcPlayer.takeSnapShot(textureView, sdcardPath);
//                }).start();
            vlcPlayer!!.takeSnapShot(textureView, picturePath)
            Toast.makeText(context, "截图完成", Toast.LENGTH_SHORT).show()
            //保存为csv
            val filePath = picturePath?.parent// 替换为你要检查的文件路径
            val file = File(filePath + "/pic.csv")
//            Log.d("oppopath","${file}")
            if (file.exists()) {
                val picname=picturePath?.name
                val row = listOf(picname, "false", "false")
                FileWriter(file, true).buffered().use { out ->
                    // Write header
                    out.write(row.joinToString(","))
                    out.newLine()
                }
            } else {
                Toast.makeText(getContext(),"文件缺失，重启",Toast.LENGTH_SHORT)
            }
            //保存结束
        }

        return root
    }

    private fun initVLCPlayer() {
        Log.d("sdfg", "打开视频")
        vlcPlayer = VLCPlayer(context)
        vlcPlayer!!.setVideoSurface(textureView)
        vlcPlayer!!.setDataSource(rtspUrl)
        vlcPlayer!!.setCallback(this)
        vlcPlayer!!.play()
    }


    override fun onDestroy() {
        super.onDestroy()
        if(vlcPlayer!=null){
            vlcPlayer!!.release()
        }
    }


    override fun onBuffering(bufferPercent: Float) {
    }

    override fun onEndReached() {
        Toast.makeText(context, "播放结束", Toast.LENGTH_SHORT).show()
    }

    override fun onError() {
        Toast.makeText(context, "播放出错", Toast.LENGTH_SHORT).show()
    }

    override fun onTimeChanged(currentTime: Long) {
    }

    override fun onPositionChanged(position: Float) {
    }

    companion object {
        //    private static final String rtspUrl = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4";
        private const val rtspUrl = "rtsp://admin:admin@192.168.1.88:554/11"
    }
}